***************************************
**** Table A1: SUPPLEMENTARY BALANCE TABLE
***************************************

use "$dir/Data/analysis_main.dta", clear

keep if survsample==1 		


cap file close sumstat
file open sumstat using "$oa/$t/Table_A1.tex", write replace
		file write sumstat "{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  \begin{tabular}{@{\extracolsep{0pt}}p{7cm}*{6}{>{\centering\arraybackslash}m{2.5cm}}@{}}   \toprule" _n
		file write sumstat " &\multicolumn{1}{c}{Mean:}&\multicolumn{1}{c}{Coef:}&\multicolumn{1}{c}{P-value} \\" _n ///
		" &\multicolumn{1}{c}{No piece-rate}&\multicolumn{1}{c}{In piece-rate}&\multicolumn{1}{c}{} \\" _n ///
		" &\multicolumn{1}{c}{rounds}&\multicolumn{1}{c}{rounds}&\multicolumn{1}{c}{} \\" _n ///
		" & (1) & (2) & (3)  \\" _n ///
		"\midrule" _n ///
		"\multicolumn{4}{l}{\textit{Panel A. Demographic Characteristics and Financial Worries}} \\ \addlinespace[5pt]  " _n 

	// panel A
	
	foreach var of varlist age_m educ read_odiya_m married anychildren caslabor_m bs_labor_7 bs_labor_30 wealth_ind_avg wealth_hi_avg{ 
	
		local varlab: variable label `var'
		
		reghdfe `var' entered_twice if pid1==1, absorb(roundgroup) cluster(pid)
		
		local coef_treat1: di %9.3f _b[entered_twice]
		local se_treat1: di %9.3f _se[entered_twice]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[entered_twice])/(_se[entered_twice])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
		
		summ `var' if e(sample) & entered_twice==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}
	// panel B
	
	// file write sumstat "\addlinespace[4pt] \multicolumn{4}{l}{\textit{Panel B. Financial Worries and Loans}} \\   \addlinespace[5pt]  " _n 
				
	foreach var of varlist worry1_m anyloans_worry bl_loanamt_m hasloan loan_moneylender  { 
		
		local varlab: variable label `var'
		
		reghdfe `var' entered_twice if pid1==1, absorb(roundgroup) cluster(pid)
	
		local coef_treat1: di %9.3f _b[entered_twice]
		local se_treat1: di %9.3f _se[entered_twice]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[entered_twice])/(_se[entered_twice])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
	
		summ `var' if e(sample) & entered_twice==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
		
		if "`var'"=="bl_loanamt_m"{
			foreach y in mean_cont1 sd_cont1 coef_treat1 se_treat1{
				local `y'=trim(string(``y'', "%9.0fc"))
			}
		}			
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}

	// panel C
	
	file write sumstat "\addlinespace[4pt] \multicolumn{4}{l}{\textit{Panel B. Baseline Performance}} \\  \addlinespace[5pt]" _n 
	
	foreach var of varlist productivity att_ind { 
	
		local varlab: variable label `var'
	
		if "`var'"=="attend" reghdfe `var' entered_twice if pidday1==1 & postcash==0, absorb(roundgroup) cluster(pid)
		else reghdfe `var' entered_twice if postcash==0, absorb(roundgroup) cluster(pid)
		
		local coef_treat1: di %9.3f _b[entered_twice]
		local se_treat1: di %9.3f _se[entered_twice]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[entered_twice])/(_se[entered_twice])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
		
		summ `var' if e(sample) & entered_twice==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}
	
	file write sumstat "\addlinespace[4pt] \multicolumn{4}{l}{\textit{Panel C. Treatment Probability}} \\  \addlinespace[5pt]" _n 
		
	foreach var of varlist cash  { 
		
		local varlab: variable label `var'
		
		reghdfe `var' entered_twice if pid1==1, absorb(roundgroup) cluster(pid)
	
		local coef_treat1: di %9.3f _b[entered_twice]
		local se_treat1: di %9.3f _se[entered_twice]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[entered_twice])/(_se[entered_twice])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
	
		summ `var' if e(sample) & entered_twice==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
			
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}		
		
	count if entered_twice==0 & pid1==1 & round<=15
	local N1 = r(N)
	count if entered_twice==1 & pid1==1 & round<=15
	local N2 = r(N)	

	
	file write sumstat "\midrule \addlinespace[5pt]   " _n  
	file write sumstat "N: workers & `N1' & `N2'    \\  " _n   
	file write sumstat "\bottomrule   \end{tabular}   }" _n           // table footer
	file close sumstat	

